"""
多任务练习
"""

import multiprocessing
import os
import time

c_list = []


def task1(count):
    print('任务1的PID = ' + str(os.getpid()), '父进程的PID = ', os.getppid())
    for x in range(count):
        print('A------', x + 1)
        c_list.append(x)
        time.sleep(2)

    print(c_list)


def task2(count):
    print('任务2的PID = ' + str(os.getpid()), '父进程的PID = ', os.getppid())
    for x in range(10, count):
        print('B------', x + 1)
        c_list.append(x)
        time.sleep(1.4)
    print(c_list)


if __name__ == '__main__':
    # 创建子进程对象
    p1 = multiprocessing.Process(target=task1, name='P1', kwargs={'count': 5})
    p2 = multiprocessing.Process(target=task2, name='P2', args=(20,))
    # 获取当前进程对象
    # mp = multiprocessing.current_process()
    # print(mp)
    # print(p1)
    # print(p2)
    # 设置守护进程,主进程结束，子进程也会自动结束
    p1.daemon = True
    p2.daemon = True
    p1.start()
    p2.start()
    time.sleep(2)
    # p2.terminate()
    # print(p1)
    # print(p2)
    print(c_list)
